"""
SqlAlchermy Column常用数据类型详解
"""
import os
import sys

path = os.path.dirname(__file__)
sys.path.append(os.path.join(path, ".."))

import enum
import random
from datetime import datetime
from faker import Faker
from sqlalchemy import Column, Integer, String, Boolean, DECIMAL, Date, DateTime, Time, Text, Enum
from dateutil.relativedelta import relativedelta
from conf.mysql_config import BASE, SESSION
from utils.random_time import gen_random_time


class PositionEnum(enum.Enum):
    front_end = '前端'
    back_end = '后端'
    whole_stack = '全栈'
    ui = 'UI'
    test = '测试'
    product = '产品'

    @staticmethod
    def keys():
        return [position.name for position in PositionEnum]

    @staticmethod
    def values():
        return [position.value for position in PositionEnum]


class CompanyStaff(BASE):
    # 1.定义数据库表名
    __tablename__ = 'company_staff'
    # 2.定义数据库字段及字段类型
    id = Column(Integer, autoincrement=True, primary_key=True)
    user_name = Column(String(10), comment='员工姓名', doc={'zh': '员工姓名'})
    sex = Column(Boolean, comment='性别', doc={'zh': '性别'})
    # 1000.00-100000.00：工资1000-10万，最长8位数，小数位2位
    probation_salary = Column(DECIMAL(8, 2), comment='试用期工资', doc={'zh': '试用期工资'})
    salary = Column(DECIMAL(8, 2), comment='转正工资', doc={'zh': '转正工资'})
    position = Column(Enum(PositionEnum), comment='职位', doc={'zh': '职位'})
    register_date = Column(Date, comment='报道日期', doc={'zh': '报道日期'})
    official_date = Column(Date, comment='转正日期', doc={'zh': '转正日期'})
    remark = Column(Text, comment='备注', doc={'zh': '备注'})
    create_date = Column(DateTime, comment='创建日期', doc={'zh': '创建日期'})
    create_time = Column(Time, comment='创建时间', doc={'zh': '创建时间'})


# 3. 映射数据库
BASE.metadata.create_all()

fake = Faker(locale='zh_CN')


def add_data():
    """新增数据"""
    company_staffs = []
    for _ in range(1000):
        salary = round(random.randint(1000, 100000), 2)
        probation_salary = round(salary * 0.8, 2)
        gen_date = gen_random_time()
        gen_date_time = datetime.strptime(gen_date.get('date'), '%Y-%m-%d %H:%M:%S')
        three_gen_date = gen_date_time + relativedelta(months=3)
        now = datetime.now()
        sex_bool = bool(random.randint(0, 1))
        if sex_bool:    # 生成男姓名
            user_name = fake.name_female()
        else:    # 生成女性名
            user_name = fake.name_male()
        company_staffs.append(CompanyStaff(user_name=user_name, sex=sex_bool,
                                           probation_salary=probation_salary, salary=salary,
                                           position=random.choice(PositionEnum.keys()),
                                           register_date=gen_date_time.date(), official_date=three_gen_date.date(),
                                           remark=f'地址：{fake.address()}',
                                           create_date=now, create_time=now.time()))
    SESSION.add_all(company_staffs)
    SESSION.commit()


if __name__ == '__main__':
    add_data()
